package ch.rheintalconnection.sysventory.technicalServices.hibernate;

import javax.persistence.*;

import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;

import ch.rheintalconnection.sysventory.domain.Scan;
import ch.rheintalconnection.sysventory.technicalServices.IDataWriter;

public class MySqlDataWriter implements IDataWriter {

	// private EntityManagerFactory emf = null;
	private EntityManager em = null;
	private EntityTransaction tx = null;

	private static Logger logger = LogManager.getLogger(MySqlDataWriter.class);

	public MySqlDataWriter() {
		
		em = MySqlDataEntity.getEm();

		tx = em.getTransaction();
	}

	public void store(Scan scan) {
		try {
			tx.begin();
			em.persist(scan);
			em.flush();
			tx.commit();
			//em.clear();

			logger.info("scan " + scan.getId() + " is stored in the database.");

		} catch (Exception e) {
			e.printStackTrace();
			tx.rollback();
		}
	}
}
